// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App.vue'
import router from './router/index.router'

// get login-info
import qs from 'qs'
import store from './store'
const query = qs.parse(location.search.substr(1))
let storage = window.sessionStorage

store.commit('login', {
  token: storage.getItem('token'),
  userId: storage.getItem('userId'),
  homeLogo: storage.getItem('operator') ? storage.getItem('operator') : 'logo',
  masterId: storage.getItem('masterId'),
  userPhone: storage.getItem('userPhone') == 'null' ? null : storage.getItem('userPhone'),
});

// location.search && store.commit('login', {
//   token: query.token,
//   userId: query.userId,
//   masterId: query.masterId,
//   userPhone: query.userPhone,
// });

// api axios
import http from './api/http'
Vue.prototype.$http = http

// flexible
import './assets/flexible/flexible.debug.js'

// iconfont 
import './assets/fonts/iconfont.css'

// components vux
import {ViewBox, TransferDom, Actionsheet, Confirm, XDialog, Popup, Group, Cell, XInput, Selector, PopupPicker, Datetime, DatetimeRange, XAddress, XButton, Icon, Countdown, Divider, CheckIcon } from "vux";
Vue.directive('transfer-dom', TransferDom)

Vue.component('view-box', ViewBox)
Vue.component('actionsheet', Actionsheet)
Vue.component('confirm', Confirm)
Vue.component('x-dialog', XDialog)
Vue.component('popup', Popup)
Vue.component('group', Group)
Vue.component('cell', Cell)
Vue.component('x-input', XInput)
Vue.component('selector', Selector)
Vue.component('popup-picker', PopupPicker)
Vue.component('datetime', Datetime)
Vue.component('datetime-range', DatetimeRange)
Vue.component('x-address', XAddress)
Vue.component('x-button', XButton)
Vue.component('countdown', Countdown)
Vue.component('divider', Divider)
Vue.component('icon', Icon)
Vue.component('check-icon', CheckIcon)

import {ToastPlugin, WechatPlugin} from 'vux'
Vue.use(ToastPlugin)
Vue.use(WechatPlugin)

// component swiper
import 'swiper/dist/css/swiper.css'
import VueAwesomeSwiper from 'vue-awesome-swiper'
Vue.use(VueAwesomeSwiper)

// component scroller
import VueScroller from 'vue-scroller'
Vue.use(VueScroller)

// webim config
import webIM from 'easemob-websdk'
import webIMConfig from './webim.config'
webIM.config = webIMConfig

Vue.prototype.$webIM  = window.WebIM = webIM
Vue.prototype.$imConn = new webIM.connection({
  isMultiLoginSessions: webIM.config.isMultiLoginSessions,
  https: typeof webIM.config.https === 'boolean' ? webIM.config.https : location.protocol === 'https:',
  url: webIM.config.xmppURL,
  heartBeatWait: webIM.config.heartBeatWait,
  autoReconnectNumMax: webIM.config.autoReconnectNumMax,
  autoReconnectInterval: webIM.config.autoReconnectInterval,
  apiUrl: webIM.config.apiURL,
  isAutoLogin: true,
})

import ap from './ap.js'
Vue.prototype.$ap = ap

// click in mobile client
import FastClick from 'fastclick'
FastClick.attach(document.body)

// is login
router.beforeEach((to, from, next) => {
  if (to.meta.requireAuth && to.path != '/login') {
    if (store.state.token) {
      next();
    } else {
      next({
        path: '/login',
        query: {
          redirect: to.fullPath
        }
      })
    }
  } else {
    next();
  }
  if(document.domain.indexOf('yidiangaoren') != -1) {
    if(_hmt) {
      if(to.path) {
        _hmt.push(['_trackPageview', '/#' + to.fullPath])
      }
    }
    next();
  }
})

// filters
Vue.filter('hasdomain', Vue.prototype.hasdomain = (value) => {
  return value ? value.indexOf('http') == -1 ? store.state.domain + value : value : require('@/assets/logo@2x.png');
})

// set maidian
if(document.domain.indexOf('yidiangaoren') != -1) {
  var _hmt = _hmt || [];
  window._hmt = _hmt; // 必须把_hmt挂载到window下，否则找不到
  (function () {
        var hm = document.createElement("script");
        hm.src = "https://hm.baidu.com/hm.js?"+ '954440d5d66ef7db324d669825943ea5'; // 百度站点id;
        var s = document.getElementsByTagName("script")[0];
        s.parentNode.insertBefore(hm, s);
  })();
}


Vue.config.productionTip = false

/* eslint-disable no-new */
new Vue({
  el: '#app-box',
  router,
  store,
  render: h => h(App)
})
